﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        #region privilege

        public bool SavePrivilege(Privilege privilege)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                EntityRepository.Save(privilege, context.DbContext);

                return EntityRepository.Commit(context.DbContext);
            }
        }

        public bool DeletePrivilege(Guid id)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                if (EntityRepository.Delete(id, context.DbContext))
                {
                    return false;
                }
                return EntityRepository.Commit(context.DbContext);
            }
        }

        public Privilege GetPrivilege(Guid id)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                return EntityRepository.Get(id, context.DbContext) as Privilege;
            }
        }

        public PagedList<Privilege> GetPrivileges(string type, int start, int limit)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                List<Privilege> list = null;
                if (string.IsNullOrEmpty(type))
                {
                    list = context.Set<Privilege>()
                        .OrderBy(p => p.Name)
                        .Take(limit)
                        .Skip(start)
                        .ToList();
                }
                else
                {
                    list = context.Set<Privilege>().Where(p => p.Type == type)
                        .OrderBy(p => p.Name)
                        .Take(limit)
                        .Skip(start)
                        .ToList();
                }

                int totalCount = context.Set<Privilege>().Where(p => p.Type == type).Count();

                PagedList<Privilege> pagedList = new PagedList<Privilege>();
                pagedList.AddRange(list);
                pagedList.Start = start;
                pagedList.Limit = limit;
                pagedList.Total = totalCount;
                return pagedList;
            }
        }

        public PagedList<Privilege> FindPrivileges(string code, int start, int limit)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                List<Privilege> list = context.Set<Privilege>().Where(p => p.Name.Contains(code))
                    .OrderBy(p => p.Name)
                    .Take(limit)
                    .Skip(start)
                    .ToList();

                int totalCount = context.Set<Privilege>().Where(p => p.Name.Contains(code)).Count();

                PagedList<Privilege> pagedList = new PagedList<Privilege>();
                pagedList.AddRange(list);
                pagedList.Start = start;
                pagedList.Limit = limit;
                pagedList.Total = totalCount;
                return pagedList;
            }
        }
        #endregion
    }
}
